<?php
function getuser_name($id)
{
    global $conn;
    $sql = "SELECT * FROM user where user.id=$id ";
    $user_result = $conn->query($sql);
    $row = $user_result->fetch_assoc();
    return $row["nickname"];
}
function getpost_num($id)
{
    global $conn;
    $sql = "SELECT * FROM post where mainpostid=$id ";
    $result = $conn->query($sql);

    return $result->num_rows;
}
function getuser_main($id)
{
    global $conn;
    $sql = "SELECT * FROM mainpost,user where mainpost.id=$id and mainpost.userid=user.id";
    $user_result = $conn->query($sql);
    $row = $user_result->fetch_assoc();
    return $row;
}
function getuser_data($id)
{
    global $conn;
    $sql = "SELECT * FROM user where user.id=$id ";
    $user_result = $conn->query($sql);
    $row = $user_result->fetch_assoc();
    return $row;
}
function get_session_user()
{
    // var_dump($_SESSION);
    if (isset($_SESSION['id'])) {
        return $_SESSION['id'];
    } else {
        return 0;
    }
}
function login($user, $password)
{
    global $conn;
    $sql = "SELECT * FROM user where user.loginname='$user' and passwords='$password' ";
    // echo ($sql);
    $user_result = $conn->query($sql);
    if ($user_result->num_rows > 0) {
        $row = $user_result->fetch_assoc();

        $_SESSION["id"] = intval($row["Id"]);

        return 1;
    }
    return 0;
}
function unlogin()
{
    $_SESSION["id"] = 0;
}
